<?php
/*
 *
 *      photo.php - v1.0.0 @07.07.2009 22:29:40 CEST
 *
 *      StudioGallery Copyright (C) 2009 Rafal Kusior (spuavick) <spuavick@interia.pl>
 *
 *      This program is free software: you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation, either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *      -----------------------------------------------------------------------------
 *
 *      Niniejszy program jest wolnym oprogramowaniem - możesz go rozpowszechniać
 *      dalej i/lub modyfikować na warunkach Powszechnej Licencji Publicznej GNU
 *      wydanej przez Fundację Wolnego Oprogramowania, według wersji 2 tej Licencji
 *      lub dowolnej z późniejszych wersji.
 *
 *      Niniejszy program rozpowszechniany jest z nadzieją, iż będzie on użyteczny -
 *      jednak BEZ ŻADNEJ GWARANCJI, nawet domyślnej gwarancji PRZYDATNOŚCI HANDLOWEJ
 *      albo PRZYDATNOŚCI DO OKREŚLONYCH ZASTOSOWAŃ. Bliższe informacje na ten temat
 *      można uzyskać z Powszechnej Licencji Publicznej GNU.
 *
 *      Kopia Powszechnej Licencji Publicznej GNU powinna zostać ci dostarczona
 *      razem z tym programem. Jeżeli nie została dostarczona, odwiedź
 *      <http://www.gnu.org/licenses/>
 */

$preU= "";

require_once('sg_settings.php');

if (preg_match('/([a-z0-9]){32}/i',$_GET['idp'])) {

	require_once('sg-include/sg-include.php');

	$r=$db->select("SELECT * FROM "._pre_."photo WHERE photo_file ='".$_GET['idp']."' AND photo_show>0;");
	$photo = $db->get_row($r,'MYSQL_ASSOC');
	if ($photo){
	$visit = $photo['photo_visit'] + 1;
	$dane = array (
					'photo_visit' => $visit
	      	);

	$photo['photo_visit'] = $visit;

	$t = $db->update_array(_pre_."photo",$dane,"photo_id=".$photo['photo_id']);


	$r = $db->select("SELECT photo_title,photo_id,photo_file FROM "._pre_."photo WHERE photo_id < ".$photo['photo_id']." AND photo_show > 0 AND photo_cat_id = ".$photo['photo_cat_id']." ORDER BY photo_id DESC;");
	$prev = $db->get_row($r,'MYSQL_ASSOC');

	if ($prev) $prev_new = '<div class="right"><a href="photo.php?idp='.$prev['photo_file'].'">'.$prev['photo_title'].'</a>&nbsp;&raquo;</div>';

	$r = $db->select("SELECT photo_title,photo_id,photo_file FROM "._pre_."photo WHERE photo_id > ".$photo['photo_id']." AND photo_show > 0 AND photo_cat_id = ".$photo['photo_cat_id']." ORDER BY photo_id ASC;");
	$next = $db->get_row($r,'MYSQL_ASSOC');

	if ($next) $next_new = '<div class="left">&laquo;&nbsp;<a href="photo.php?idp='.$next['photo_file'].'">'.$next['photo_title'].'</a></div>';

	if ($prev || $next) $navi = '<div class="pagination">'.$next_new.$prev_new.'<div class="clr"></div></div>';

	if ( $sghook->hooks_exist('navigation') ) {
	    $smarty->assign('navi',$sghook->execute_hooks('navigation',$navi));
	} else {
	    $smarty->assign('navi',$navi);
	}

	/** komentarze do zdjęcia */
	if ($conf['conf_comments'] > 0) {

  		$r = $db->select("SELECT * FROM "._pre_."comments WHERE com_photo_id = ".$photo['photo_id']." AND com_show>0 ORDER BY com_id DESC;");

  		while($com = $db->get_row($r,'MYSQL_ASSOC')) {

		    $com['com_desc'] = dataFromDb($com['com_desc']);
    		$com['com_www'] = createURI($com['com_www']);

    		if ($com['com_www'] ) {
				$com['user_info'] = '<div class="username">
										<a href="'.$com['com_www'].'">'.$com['com_user'].'</a>
									</div>
									<div class="datecom">'.date("d/m/Y (H:i)", $com['com_date']).'</div>';

    		} else {
				$com['user_info'] = '<div class="username">'.$com['com_user'].'</div>
									<div class="datecom">'.date("d/m/Y (H:i)", $com['com_date']).'</div>';

  			}
			$comm[] = $com;

		}
		    $comile = count($comm);

  			$smarty->assign('comile',$comile);
  			$smarty->assign('comm',$comm);
  			$smarty->assign('comwhat',1);
  			$smarty->assign('id',$photo['photo_id']);
		}
		/** exif */
		if ($conf['conf_exif'] > 0 && $photo['photo_exif']) $smarty->assign('exif', true);

		/** ocena zdjęcia */
		if ($conf['conf_votes'] > 0) {

  			if ($photo['photo_votes']>0) {

    			$srednia_ocen = round($photo['photo_rate']/$photo['photo_votes'],2);
    			$votes = $photo['photo_votes'];
				$srednia_ocen = str_replace(",",".",$srednia_ocen);

  			} else {

    			$srednia_ocen = 0;
    			$votes = 0;

			}

			$photo['oceny'] = '<div class="ocenytxt">'.number_format(($srednia_ocen), 2, ',', ' ').'&nbsp;/&nbsp;'.$votes.'</div>';
			$photo['rating'] = '<div class="rating" id="rate"></div>';
			$rating = '
				<script type="text/javascript" language="javascript">
    				// <!-- <![CDATA[
    					$(\'#rate\').rating(\'rating-vote.php?idp='.$photo['photo_id'].'&key='.$key.'\', {maxvalue:5, curvalue:'.$srednia_ocen.', increment: .5});
    				// ]]> -->
  				</script>';

				$smarty->assign('rating',$rating);

		}

		/** nawigacja */
		if ($conf['conf_start_page'] != 2) {

			$galeria = '<a href="gallery.php">';
			$galeria .=_('galeria');
			$galeria .= '</a> &raquo; ';

		} else {

			$galeria = '';

		}

		$photo['date_day'] = date("d",$photo['photo_date']);
		$photo['date_month'] = dataPublic(mdate2date($photo['photo_date']),0);
		$photo['date_year'] = date("Y",$photo['photo_date']);
		$photo['date_hour'] = date("H",$photo['photo_date']);
		$photo['date_minutes'] = date("i",$photo['photo_date']);

		$link = $galeria.raquoMenu(subCatMenu($photo['photo_cat_id']));
		$photo['photo_img'] = '<a rel="lightbox['.$photo['photo_id'].']" title="'.$photo['photo_title'].'" href="photos/'.$photo['photo_file'].'.jpg"><img src="photos/'.$photo['photo_file'].'_p.jpg" alt="'.$photo['photo_title'].'" /></a>';
	}
		$info = _('Przykro mi, ale podane zdjęcie nie istnieje...');
		$back = _('&laquo; powrót');
		if ( $sghook->hooks_exist('photoinfo') )
			$smarty->assign('photoinfo',$sghook->execute_hooks('photoinfo',$photo));
		$smarty->assign('photo',$photo);
		if ( $sghook->hooks_exist('cat_gallery') ) {
		    $smarty->assign('subpage',$sghook->execute_hooks('cat_gallery',$link));
		} else {
		    $smarty->assign('subpage',$link);
		}
		$smarty->assign('linkback','<div class="empty">'.$info.'<div class="link-back"><a href="#" onclick="linkback(); return false;">'.$back.'</a></div></div>');

} else {

	header("Location: ./index.php");

}

$smarty -> display("photo.tpl");

?>
